#include "SampleNode.h"

CAE_HANDLE cae = NULL;

RT_RET RTRockitDemoNode::open(RTTaskNodeContext *context)
{
    RT_RET err = RT_OK;
    RtMetaData *meta = context->options();
    // RTNodeDemoCtx *ctx = reinterpret_cast<RTNodeDemoCtx *>(mCtx);
    // if (RT_NULL == ctx) {
    // RT_LOGE("context is NULL, please alloc RTNodeDemoCtx in construct");
    // return RT_ERR_INIT;
    // }
    // check parameters
    INT32 sampleRate = 0;
    if (!meta->findInt32("opt_samaple_rate", &sampleRate))
    {
        RT_LOGE("samplerate not find");
        return RT_ERR_INIT;
    }
    //ctx->mSampleRate = sampleRate;
    // the other init steps

    // int CAEInit(struct AiotOS * param);

    return err;
}

// 用于节点创建， 该函数指针将存于RTNodeStub.mCreateObj中
RTTaskNode *createRockitDemoNode()
{
    return new RTRockitDemoNode();
}

RT_RET RTRockitDemoNode::doAIProcess(RTMediaBuffer *src, RTMediaBuffer *dst)
{

    int once_size = 512 * sizeof(short);

    CAEAudioWrite(cae, (char *)src, once_size);

    return RT_OK;
}

// 节点处理函数，将输入的数据处理， 然后输出到下级节点
RT_RET RTRockitDemoNode::process(RTTaskNodeContext *context)
{
    RTMediaBuffer *inputBuffer = RT_NULL;
    RTMediaBuffer *outputBuffer = RT_NULL;

    // 判断输入是否为空
    while (!context->inputIsEmpty())
    {
        // 取出输入的buffer
        inputBuffer = context->dequeInputBuffer();
        // 取出一块未被使用的输出Buffer
        outputBuffer = context->dequeOutputBuffer(RT_TRUE, inputBuffer->getLength());
        // 执行处理函数
        doAIProcess(inputBuffer, outputBuffer);
        // 设置输出buffer的范围
        outputBuffer->setRange(0, inputBuffer->getLength());
        // 标记EOS
        if (inputBuffer->isEOS())
        {
            outputBuffer->getMetaData()->setInt32(kKeyFrameEOS, 1);
        }
        // 输入Buffer使用完成，调用释放
        inputBuffer->release();
        // 将输出buffer带出，完成处理流程
        context->queueOutputBuffer(outputBuffer);
    }

    return RT_OK;
}

//节点信息存根， 用于完成节点注册
RTNodeStub node_stub_rockit_demo{
    // 节点uid， 节点的唯一标识符 (0~1000)
    .mUid = kStubRockitDemo,
    // 节点名， 主要用于节点查找、创建
    // corp_role_name，命名保证唯一
    .mName = "rockit_demo",
    // 版本号
    .mVersion = "v1.0",
    // 节点创建方法; 改成宏定义
    .mCreateObj = createRockitDemoNode,
    .mCapsSrc = {"video/x-raw", RT_PAD_SRC, {RT_NULL, RT_NULL}},
    .mCapsSink = {"video/x-raw", RT_PAD_SINK, {RT_NULL, RT_NULL}},
};

RT_NODE_FACTORY_REGISTER_STUB(node_stub_rockit_demo);
